const rp = require('request-promise');

module.exports = class extends think.Controller {
  async __before() {
    // 根据token值获取用户id
    this.header('Cache-Control', 'no-cache')
    this.ctx.state.token = this.ctx.header['x-nideshop-token'] || '';

    const tokenSerivce = think.service('token', 'api');
    this.ctx.state.userId = await tokenSerivce.getUserId(this.ctx.state.token);
    this.ctx.state.rip=this.ctx.header['x-real-ip'] || '';
    const publicController = this.config('publicController');
    const publicAction = this.config('publicAction');
    // 如果为非公开，则验证用户是否登录
    const controllerAction = this.ctx.controller + '/' + this.ctx.action;
    if (!publicController.includes(this.ctx.controller) && !publicAction.includes(controllerAction)) {
      if (this.ctx.state.userId <= 0) {
        return this.fail(401, '请先登录');
      }
    }

    //游戏玩法的用户邀请码
    this.ctx.state.idle_shop_invite_code = 303000;
  }

  /**
   * 获取用户邀请码
   * @returns {Number}
   */
  getInviteCode() {
    this.ctx.state.idle_shop_invite_code=this.ctx.state.idle_shop_invite_code+1;
    return this.ctx.state.idle_shop_invite_code;
  }

  /**
   * 获取时间戳
   * @returns {Number}
   */
  getTime() {
    return parseInt(Date.now() / 1000);
  }

  formtime (date,fmt) {//加入
    var o = {
      "M+": date.getMonth() + 1, //月份
      "d+": date.getDate(), //日
      "h+": date.getHours(), //小时
      "m+": date.getMinutes(), //分
      "s+": date.getSeconds(), //秒
      "q+": Math.floor((date.getMonth() + 3) / 3), //季度
      "S": date.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  }

  /**
   * 获取当前登录用户的id
   * @returns {*}
   */
  getLoginUserId() {
    return this.ctx.state.userId;
  }

  async vcaptcha(token,wallet_address,ip,scene){//下次加
    console.log(scene);
    if(think.isEmpty(token)){
      await this.model('gameuser').where({wallet_address:wallet_address}).update({warn_account:'无滑动验证'})
      console.log(`${scene==1?'login':'haverest'} token为空--${this.formtime(new Date(),"MM-dd hh:mm:ss")}-->${wallet_address}`)
      return false
    }
    var data;
    if(this.ctx.state.rip == "")
    {
      data={
        id:scene!=4?'5fdb55d9e1874d214d49afb3':'5e6a0182ecbf53b8962e70fb',
        secretkey:scene!=4?"d5b65f92fa1c4409a01f6900d5403669":"886964da731a407cb5acd255388f2d4d",//'b6b4364d1f0a4ac7b02116a39cd36af8':'886964da731a407cb5acd255388f2d4d',
        scene:scene,
        token:token,
        ip:ip
      }
    }
    else
    {
      data={
        id:scene!=4?'5fe090519f63f1307ef46f92':'5e6a0182ecbf53b8962e70fb',
        secretkey:scene!=4?"2ebc9121ee60450a8cd0024edba78f23":"886964da731a407cb5acd255388f2d4d",//'b6b4364d1f0a4ac7b02116a39cd36af8':'886964da731a407cb5acd255388f2d4d',
        scene:scene,
        token:token,
        ip:ip
      }
    }

    const options={
      method:'POST',
      uri:`http://0.vaptcha.com/verify`,
      header: {
        'content-type': 'application/json'
      },
      body: data,
      json: true
    }
    try{
      // console.log(token)
      const result=await rp(options)
      console.log(result)
      // if(wallet_address=='0x15599DC0c9cF80c90bde2E6715e75EB9c2dEEb01'){
      // console.log(wallet_address,result)
      // }
      return  result.success==1&&result.score>=80
    }catch(e){
      console.log('验证码服务器请求失败')
      return false
    }

  }
};
